<?php

namespace App\Controller\admin;

use Hyperf\DbConnection\Db;
use App\Model\admin\AdminuserModel;
use App\Exception\ValidateException;


class BaseController extends CommonController {
	
	
	/*
 	* @Description 定义二开的方法加入权限系统
 	*/
	public function getBaseFuns(){
		$config = [[
			'title' => '基础操作',
			'access' => 'admin/Base',
			'sortid'=>1,
			'children'=>[
				[
					'title'	=> '重置密码',
					'access'	=>  '/admin/Base/resetPwd',
				],
			],
		]];
		
		return $config;
	}
	
	
	/*
 	* @Description 图片管理列表
 	*/
	function fileList(){
		$limit  = $this->request->input('limit', 20);

		$where = [];
        $where['type'] = 1;
		$field = 'id,filepath,hash,create_time';

		$res = Db::table('file')->where($where)->select(explode(',',$field))->orderBy('id','desc')->paginate($limit)->toArray();

		$data['status'] = 200;
		$data['data'] = $res;
		return json($data);
	}


	/*
 	* @Description  删除图片
 	*/
	function deleteFile(){
		$filepath =  $this->request->input('filepath');
		
		if(!$filepath){
			throw new ValidateException("请选择文件");
		}
		
		if(!is_array($filepath)){
			$filepath = [$filepath];
		}
		
		event(new \App\Event\DeleteFileEvent($filepath));
		
		Db::table('file')->whereIn('filepath',$filepath)->delete();
	
		return json(['status'=>200,'msg'=>'操作成功']);
	}
	
	
	/*
 	* @Description  重置密码
 	*/
	public function resetPwd(){
		$password = $this->request->input('password');
		
		if(empty($password)){
			throw new ValidateException("密码不能为空");
		};
		
		$data['pwd'] = md5($password.config('my.password_secrect'));
		
		$res = AdminuserModel::where('user_id',session()->get('admin')->user_id)->update($data);
		
		return json(['status'=>200,'msg'=>'操作成功']);
	}
	
	
	/*
 	* @Description  清除缓存
 	*/
	public function clearCache(){
		try{
			cache()->clear();
		}catch(\Exception $e){
			throw new ValidateException($e->getMessage());
		}
		return json(['status'=>200,'msg'=>'操作成功']);
	}
	
	
	
	/*
 	* @Description 左侧菜单
 	*/
	public function getMenu(){
		$menu =  $this->getBaseMenus();

		$order_array = array_column($menu, 'sortid');			//数组排序 根据sortid 正序
		array_multisort($order_array,SORT_ASC,$menu );
		$roleInfo = object2array(session()->get('admin'));
		$mymenu = $this->getMyMenus($roleInfo,$menu);
		return json(['status'=>200,'data'=>$mymenu]);
	}
	
	
	//获取当前角色的菜单
	private function getMyMenus($roleInfo,$totalMenus){
		if($roleInfo['role_id'] == 1){
			return $totalMenus;
		}
		$tree = [];
		foreach($totalMenus as $key=>$val){
			if(in_array($val['url'],$roleInfo['access'])){
				$tree[] = array_merge($val,['children'=>$this->getMyMenus($roleInfo,$val['children'])]);
			}
		}
		return array_values($tree);
	}
	
	
	//返回当前应用的菜单列表
	protected function getBaseMenus(){
		$field = 'node_id,pid,title,status,icon,sortid,path';
		$list = Db::table('node')->select(explode(',',$field))->where('status',1)->where('type',1)->orderBy('sortid','asc')->get();
		if($list){
			foreach($list as $key=>$val){
				$menus[$key]['node_id'] = $val->node_id;
				$menus[$key]['pid'] = $val->pid;
				$menus[$key]['title'] = $val->title;
				$menus[$key]['sortid'] = $val->sortid;
				$menus[$key]['icon'] = $val->icon ? $val->icon : 'el-icon-menu';
				$menus[$key]['url'] = $val->path;
			}
			return _generateListTree($menus,0,['node_id','pid']);
		}
	}
	
	
	public function getRoleMenus(){
		$menu = $this->getNodeMenus(0);
		$order_array = array_column($menu, 'sortid');			//数组排序 根据sortid 正序
		array_multisort($order_array,SORT_ASC,$menu );

		return json(['status'=>200,'menus'=>$menu]);
	}
	
	
	//权限系统获取菜单
	private function getNodeMenus($pid){
		$field = 'node_id,title,pid,status,icon,sortid,path';
		$list = Db::table('node')->select(explode(',',$field))->where('pid',$pid)->orderBy('sortid','asc')->get();
		if($list){
			foreach($list as $key=>$val){
				$menus[$key]['sortid'] = $val->sortid;
				$menus[$key]['access'] = $val->path;
				$menus[$key]['title'] = $val->title;
				$sublist = Db::table('node')->select(explode(',',$field))->where('pid',$val->node_id)->orderBy('sortid', 'asc')->get();
				if($sublist){
					$menus[$key]['children'] = $this->getNodeMenus($val->node_id);
				}
			}
			return array_values($menus);
		}
	}

}
